package com.zzz.gp.service.analyse;

import com.zzz.gp.bean.GpHistory;
import com.zzz.gp.dao.GpMapper;
import com.zzz.gp.utils.FileUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: ZZZ
 * @time: 2021/12/9 21:53
 */
@Slf4j
@Service
public class A2Service {
    @Autowired
    private GpMapper gpMapper;
    @Value("${A2}")
    private String path1;

    @Async("myAsync")
    public void init() {
        DateTimeFormatter dtf=DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String today=dtf.format(LocalDateTime.now());
        String path="/home/"+today+path1;

        LocalDateTime s = LocalDateTime.now();
        List<String> adds = new ArrayList<>();
        int limit = 2;//查询三条
        Map<String, List<GpHistory>> map = new HashMap<>();
        List<GpHistory> result = gpMapper.getHistoryData2(limit);
        result.forEach(x -> {
            if (map.containsKey(x.getCode())) {
                List<GpHistory> his = map.get(x.getCode());
                his.add(x);
            } else {
                List<GpHistory> his = new ArrayList<>();
                his.add(x);
                map.put(x.getCode(), his);
            }
        });
        for (Map.Entry<String, List<GpHistory>> entry : map.entrySet()) {
            String code = entry.getKey();
            List<GpHistory> list = entry.getValue();
            A2(code, list, adds);
        }
        if (adds.size() > 0) {
            StringBuilder sb = new StringBuilder("");
            adds.forEach(x -> {
                sb.append(",").append(x);
            });
            FileUtil.txtWriter(path, sb.substring(1));
        }
        LocalDateTime e = LocalDateTime.now();
        long seconds = Duration.between(s, e).getSeconds();
        log.info("A2分析 用时{}秒,符合数量{}支", seconds, adds.size());
        log.info(adds.toString());
    }

    public void A2(String code, List<GpHistory> list, List<String> adds) {
        int n = 3;
        if (list.size() != n) {
            return;
        }

        double c = Double.parseDouble(list.get(2).getIncrease_p());
        if (c < -1.0 || c > 2.0) {
            return;
        }

        double b = Double.parseDouble(list.get(1).getIncrease_p());
        if (b < 0 || b > 2) {
            return;
        }

        double a = Double.parseDouble(list.get(0).getIncrease_p());
        if (a < 0 || a > 2) {
            return;
        }

//        for (int i = 0; i < 3; i++) {
//            double money = Double.parseDouble(list.get(i).getMoney());
//            if (money < 10000.0) {
//                return;
//            }
//        }

        adds.add(code);

    }
}

